package cn.exrick.manager.controller;

import cn.exrick.common.pojo.Result;
import cn.exrick.common.pojo.ZTreeNode;
import cn.exrick.common.utils.ResultUtil;
import cn.exrick.manager.pojo.TbOrganization;
import cn.exrick.manager.service.OrganizationService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@Api(description = "部门信息")
public class OrganizationController {

    @Autowired
    private OrganizationService organizationService;

    @RequestMapping(value = "/organization/list",method = RequestMethod.GET)
    @ApiOperation(value = "通过父ID获取部门列表")
    public List<ZTreeNode> getOrganizationList(@RequestParam(name="id",defaultValue = "0") int parentId){

        List<ZTreeNode> list=organizationService.getOrganizationList(parentId);
        return list;
    }

    @RequestMapping(value = "/organization/add",method = RequestMethod.POST)
    @ApiOperation(value = "添加部门")
    public Result<Object> addOrganization(@ModelAttribute TbOrganization tbOrganization){

        organizationService.addOrganization(tbOrganization);
        return new ResultUtil<Object>().setData(null);
    }

    @RequestMapping(value = "/organization/update",method = RequestMethod.POST)
    @ApiOperation(value = "编辑部门")
    public Result<Object> updateOrganization(@ModelAttribute TbOrganization tbOrganization){

        organizationService.updateOrganization(tbOrganization);
        return new ResultUtil<Object>().setData(null);
    }

    @RequestMapping(value = "/organization/del/{id}",method = RequestMethod.DELETE)
    @ApiOperation(value = "删除部门")
    public Result<Object> deleteOrganization(@PathVariable Long id){

        organizationService.deleteOrganization(id);
        return new ResultUtil<Object>().setData(null);
    }

    @RequestMapping(value = "/organization/getBalanceNotEnoughOrgList",method = RequestMethod.POST)
    public Result<List<TbOrganization>> getBalanceNotEnoughOrgList(){
        List<TbOrganization> orgList = organizationService.getBalanceNotEnoughOrgList();
        return new ResultUtil<List<TbOrganization>>().setData(orgList);
    }

}
